import domain.Emp;
import util.JDBCutils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Emp> emps = new Test().findall2();
        System.out.println(emps);
    }

    public List<Emp> findall() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet resultSet = null;
        List<Emp> list = null;
        try {
            conn = JDBCutils.getConnection();
            String sql = "select * from emp";
            stmt = conn.createStatement();
            resultSet = stmt.executeQuery(sql);
            list = new ArrayList<Emp>();
            while (resultSet.next()) {
                Emp emp = new Emp();
                int id = resultSet.getInt(1);
                String ename = resultSet.getString(2);
                int job_id = resultSet.getInt(3);
                int mgr = resultSet.getInt(4);
                Date joindate = resultSet.getDate(5);
                double salary = resultSet.getDouble(6);
                double bonus = resultSet.getDouble(7);
                int dept_id = resultSet.getInt(8);
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                emp.setDept_id(dept_id);
                list.add(emp);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCutils.close(resultSet, stmt, conn);
        }
        return list;
    }

    public List<Emp> findall2() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet resultSet = null;
        List<Emp> list = null;
        try {
            conn = JDBCutils.getConnection();
            String sql = "select * from emp";
            stmt = conn.createStatement();
            resultSet = stmt.executeQuery(sql);
            Emp emp = new Emp();
            list = new ArrayList<Emp>();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String ename = resultSet.getString(2);
                int job_id = resultSet.getInt(3);
                int mgr = resultSet.getInt(4);
                Date joindate = resultSet.getDate(5);
                double salary = resultSet.getDouble(6);
                double bonus = resultSet.getDouble(7);
                int dept_id = resultSet.getInt(8);
                emp.setId(id);
                emp.setEname(ename);
                emp.setJob_id(job_id);
                emp.setMgr(mgr);
                emp.setJoindate(joindate);
                emp.setSalary(salary);
                emp.setBonus(bonus);
                emp.setDept_id(dept_id);
                list.add(emp);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCutils.close(resultSet, stmt, conn);
        }
        return list;
    }
}